{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# numpy基本函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.54218191  0.34978949  1.03127924]\n",
      " [ 0.66925717 -0.51054758  1.1237048 ]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.random.randn(2, 3) # 生成2行3列的随机数\n",
    "print(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.  1.  2.]\n",
      " [ 1. -0.  2.]]\n"
     ]
    }
   ],
   "source": [
    "print(np.ceil(arr1)) # 向上取整"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1.  0.  1.]\n",
      " [ 0. -1.  1.]]\n"
     ]
    }
   ],
   "source": [
    "print(np.floor(arr1)) # 向下取整"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1.  0.  1.]\n",
      " [ 1. -1.  1.]]\n"
     ]
    }
   ],
   "source": [
    "print(np.rint(arr1)) # 四舍五入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False False False]\n",
      " [False False False]]\n"
     ]
    }
   ],
   "source": [
    "print(np.isnan(arr1)) # 空值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.29396122 0.12235269 1.06353687]\n",
      " [0.44790517 0.26065883 1.26271247]]\n"
     ]
    }
   ],
   "source": [
    "print(np.multiply(arr1, arr1)) #  矩阵乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 1. 1.]\n",
      " [1. 1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "print(np.divide(arr1, arr1)) # 矩阵除法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1  1  1]\n",
      " [ 1 -1  1]]\n"
     ]
    }
   ],
   "source": [
    "print(np.where(arr1 > 0, 1, -1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 去重函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n",
      "[[1 2 3]\n",
      " [2 3 4]\n",
      " [3 4 5]]\n"
     ]
    }
   ],
   "source": [
    "arr2 = np.array([[1,2,3], [2, 3,4],[3,4,5]])\n",
    "print(np.unique(arr2))\n",
    "print(arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 排序函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 3 2]\n",
      " [5 4 3]]\n",
      "[[1 2 3]\n",
      " [2 3 4]\n",
      " [3 4 5]]\n",
      "[[1 2 2]\n",
      " [4 3 3]\n",
      " [5 4 3]]\n",
      "[[1 2 3]\n",
      " [2 3 4]\n",
      " [3 4 5]]\n"
     ]
    }
   ],
   "source": [
    "arr3 = np.array([[1,2,3], [4,3,2],[5,4,3]])\n",
    "print(arr3)\n",
    "print(np.sort(arr3)) # 排序\n",
    "print(np.sort(arr3, axis=0)) # 按列排序\n",
    "print(np.sort(arr3, axis=1)) # 按行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ai",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
